package controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Person;
import model.UserAccount;
import util.Log;
import validator.PersonValidator;
import web.JSONMessage;
import db.ImbiConnection;

public class PersonController extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if(request.getSession().getAttribute("user") != null){
			int edit;
			try{
				edit = Integer.parseInt(request.getParameter("edit"));
				if(edit <= 0) throw new NumberFormatException();
				handleEdit(request, response, edit);
			} catch(NumberFormatException | NullPointerException e){
				handleViewAll(request, response);
			}						
		} else {
			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		}				
	}

	private void handleViewAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("person", ImbiConnection.getPersonDAO().findAll());
		request.getRequestDispatcher("/WEB-INF/person.jsp").forward(request, response);
	}

	private void handleEdit(HttpServletRequest request,
			HttpServletResponse response, int edit) throws ServletException,
			IOException {
		request.setAttribute("person", ImbiConnection.getPersonDAO().find(edit));
		if(request.getAttribute("person") != null){
			request.getRequestDispatcher("/WEB-INF/personedit.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("/WEB-INF/error.jsp").forward(request, response);
		}
	}	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		UserAccount u = (UserAccount) request.getSession().getAttribute("user");
		if(u != null){			
			PersonValidator validator = new PersonValidator(
				request.getParameter("id"),
				request.getParameter("first_name"),
				request.getParameter("last_name"),
				request.getParameter("identity"),
				request.getParameter("birthday")
			);		
			if(validator.isValid()){
				Person p = ImbiConnection.getPersonDAO().find(validator.getId());
				if(p != null){				
					p.setUpdated(new Date());
					p.setUpdatedBy(u.getEmployee().getId());
					p.setFirstName(validator.getFirstName());
					p.setLastName(validator.getLastName());
					p.setIdentity(validator.getIdentity());
					p.setBirthday(validator.getBirthday());				
					ImbiConnection.getPersonDAO().update(p);
					response.getWriter().write(JSONMessage.message("Update Successful!"));
					Log.write(u.getUsername()+" updated "+p);
				} else {			
				   response.getWriter().write(JSONMessage.errorMessage("Invalid parameters!"));
				}
			} else response.getWriter().write(JSONMessage.errorMessage("Invalid parameters!"));
		} else {
			response.getWriter().write(JSONMessage.errorMessage("Oh no!"));
		}	
	}
}